package com.woniuxy.ams.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.ICompanyTemplate2AssessItemService;
import com.woniuxy.ams.service.ICompanyTemplate2DeptService;
import com.woniuxy.ams.service.ICompanyTemplateService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 公司考核模板controller
 */
@RestController
@RequestMapping("/companyTemplate")
public class CompanyTemplateController {

    /**
     * 注入service
     */
    @Resource
    ICompanyTemplateService companyTemplateServiceImpl;
    @Resource
    ICompanyTemplate2DeptService companyTemplate2DeptServiceImpl;
    @Resource
    ICompanyTemplate2AssessItemService companyTemplate2AssessItemServiceImpl;

    /**
     * 新增公司考核模板
     * @param templateName
     * @param type
     * @param creatorId
     * @return
     */
    @RequestMapping("/addTemplate")
    public ResultEntity<?> addTemplate(@RequestParam("tempName") String templateName,
                                       @RequestParam("type") String type,
                                       @RequestParam("creator") String creatorId,
                                       @RequestParam("depts") String depts,
                                       @RequestParam("assessItems") String assessItems){
        System.out.println("t"+templateName+"ty"+type+"c"+creatorId+"d"+depts+"a"+assessItems);
        //实例创建人对象，将创建人对象放入模板对象
        User user = new User();
        user.setId(Integer.parseInt(creatorId));
        CompanyTemplate template
                = new CompanyTemplate(null, templateName, type, user,null,null,null);
        //列表实例，第一个列表放模板-部门对象，第二个列表放模板-考核计划对象
        List<CompanyTemplate2Dept> template2Depts = new ArrayList<>();
        List<CompanyTemplate2AssessItem> template2AssessItems = new ArrayList<>();
        //调用公司模板的service层，保存模板，并且将模板和部门的关系，模板和考核项的关系存入到两种中间表中
        int rows = companyTemplateServiceImpl.addTemplate(template);
        if (rows != 0){
            String[] deptIds = depts.split(",");
            List<String> deptIdList = Arrays.asList(deptIds);
            String[] assessItemIds = assessItems.split(",");
            List<String> assessIdList = Arrays.asList(assessItemIds);
            for (String dept : deptIdList) {
                template2Depts = saveTemplate2Dept(template, template2Depts, dept);
            }
            for (String assessItem : assessIdList) {
                template2AssessItems = saveTemplate2AssessItem(template, template2AssessItems, assessItem);
            }
            //调用模板对应部门的service层和模板对应考核计划的service层进行新增数据
            companyTemplate2DeptServiceImpl.addTemplate2Dept(template2Depts);
            companyTemplate2AssessItemServiceImpl.addTemplate2AssessItem(template2AssessItems);

        }else {
            return new ResultEntity<>(400, "新增失败", null);
        }
        return new ResultEntity<>(200,"新增成功",null);

    }

    /**
     * 查询所有公司考核模板(分页)
     * @return
     */
    @RequestMapping("/findAllTemplate")
    public  ResultEntity<PageInfo> findAllTemplate(@RequestBody PageRequest pageRequest){
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getLimit());
        List<CompanyTemplate> templates = companyTemplateServiceImpl.findAllTemplate();
        PageInfo<?> pageInfo = new PageInfo<>(templates);
        if(templates.size() != 0){
            return new ResultEntity<PageInfo>(200,"查询成功",pageInfo);
        }else {
            return new ResultEntity<PageInfo>(400, "查询失败", null);
        }

    }

    /**
     * 更改公司考核模版
     * @return
     */
    @RequestMapping("/updateTemplate")
    public ResultEntity<?> updateTemplate(CompanyTemplate template,
                                       @RequestParam("updatedId") String updatedId,
                                       @RequestParam("depts") String depts,
                                       @RequestParam("assessItems") String assessItems){
        //生成更改用户对象，放入模板实例，调用templateService进行公司模板表的更改
        User user = new User();
        List<Dept> depts1 = new ArrayList<>();
        List<AssessItem> assessItems1 = new ArrayList<>();
        user.setId(Integer.parseInt(updatedId));
        template.setUpdated(user);
        int rows = companyTemplateServiceImpl.updateTemplate(template);
        if (rows != 0){
            String[] deptsIds = depts.split(",");
            String[] assessIds = assessItems.split(",");
            List<String> assessList = Arrays.asList(assessIds);
            List<String> deptsList = Arrays.asList(deptsIds);
            //遍历生成部门对象，并且加入depts1列表
            for (String dept : deptsList) {
                Integer did = Integer.parseInt(dept);
                Dept d = new Dept();
                d.setId(did);
                depts1.add(d);
            }
//            //遍历生成考核计划对象，并且加入assessItems1列表
            for (String assessItem : assessList) {
                Integer aid = Integer.parseInt(assessItem);
                AssessItem aI = new AssessItem();
                aI.setId(aid);
                assessItems1.add(aI);
            }
            //此处逻辑：修改模板对应的部门和考核计划时，先删除被此时修改的模板对应的部门和考核计划，再执行新增
            //后期可优化：更改表结构，进行软删除
            companyTemplate2DeptServiceImpl.deleteTemplate2Dept(template);
            companyTemplate2AssessItemServiceImpl.deleteTemplate2AssessItem(template);
            companyTemplate2DeptServiceImpl.updateTemplate2Dept(template, depts1);
            companyTemplate2AssessItemServiceImpl.updateTemplate2AssessItem(template, assessItems1);
        }else {
            return new ResultEntity<>(400, "修改失败", null);
        }
        return new ResultEntity<>(200, "修改成功", null);
    }

    /**
     * 不分页查询所有模板
     * @return
     */
    @RequestMapping("/findTemplates")
    public ResultEntity<List<CompanyTemplate>> findTemplates(){
        List<CompanyTemplate> templates = companyTemplateServiceImpl.findAllTemplate();
        if(templates.size() != 0){
            return new ResultEntity<>(200, "查询成功", templates);
        }else {
            return new ResultEntity<>(400, "查询失败", null);
        }
    }


    /**
     * List存放模板对应考核计划实体类
     * @param template
     * @param template2AssessItems
     * @param assessItem
     * @return
     */
    private List<CompanyTemplate2AssessItem> saveTemplate2AssessItem(CompanyTemplate template, List<CompanyTemplate2AssessItem> template2AssessItems, String assessItem) {
        //考核计划实例，并且与模板实例放入模板对应考核计划实体类中
        AssessItem assessItem1 = new AssessItem();
        assessItem1.setId(Integer.parseInt(assessItem));
        CompanyTemplate2AssessItem template2AssessItem = new CompanyTemplate2AssessItem(template, assessItem1);
        //将模板对应考核计划实体类存入List并返回
        template2AssessItems.add(template2AssessItem);
        return template2AssessItems;

    }

    /**
     * List存放模板对应考核部门实体类
     * @param template
     * @param template2Depts
     * @param dept
     * @return
     */
    public List<CompanyTemplate2Dept> saveTemplate2Dept(CompanyTemplate template,List<CompanyTemplate2Dept> template2Depts,String dept){
        //部门实例，并且与模板实例放入模板对应部门实体类中
        Dept d = new Dept();
        d.setId(Integer.parseInt(dept));
        CompanyTemplate2Dept companyTemplate2Dept = new CompanyTemplate2Dept(template, d);
        //将模板对应考核部门实体类存入List并返回
        template2Depts.add(companyTemplate2Dept);
        return template2Depts;
    }

}
